package com.example.myapplication;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.viewpager.widget.PagerAdapter;

public class ViewPagerAdapter extends PagerAdapter {
    private Context mContext;
    public ViewPagerAdapter(Context context) {
        mContext = context;
    }

    // 返回页面的个数
    @Override
    public int getCount() {
        return 3;
    }

    @Override
    public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
        return view == object;
    }

    // 创建每一页的内容
    @NonNull
    @Override
    public Object instantiateItem(@NonNull ViewGroup container, int position) {
        // 加载布局，view就代表cart_listview_item布局
        View view;

        if(position == 0) {
            // 第1个页面
            view = LayoutInflater.from(mContext).inflate(R.layout.layout, container, false);
        } else if(position == 1) {
            // 第2个页面
            view = LayoutInflater.from(mContext).inflate(R.layout.activity_main, container, false);
        } else {
            // 第3个页面
            view = LayoutInflater.from(mContext).inflate(R.layout.activity_bmi, container, false);
        }
        container.addView(view);  // 把view加入到根组件当中
        Log.i("tag1", position + "  add");
        return view;
    }

    @Override
    public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
        container.removeView((View) object);
        Log.i("tag1", position + "  remove");
    }
}
